package com.aaa.lib_processor;

import com.aaa.lib_annotation.AnnotationTest;
import com.google.auto.service.AutoService;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeSpec;

import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.TypeElement;

@AutoService(Processor.class)
public class MyProcessor extends AbstractProcessor {

    @Override
    public Set<String> getSupportedAnnotationTypes() {
        Set<String> annotations = new LinkedHashSet<>();
        annotations.add(AnnotationTest.class.getCanonicalName());
        return annotations;
    }

    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
        // 创建Java类【你的类名】
        TypeSpec autoClass = TypeSpec.classBuilder("AutoClass").build();

        // 创建Java文档【这里定义了你的包名，随便写即可】
        JavaFile javaFile = JavaFile.builder("com.apt.demo", autoClass).build();

        // 将文档写入
        try {
            javaFile.writeTo(processingEnv.getFiler());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
//        原文链接：https://blog.csdn.net/u010976213/article/details/91999309
    }
}
